﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIObjects.TreeViewInterfaces;
using AbstraX.ClientInterfaces;
using System.Collections.ObjectModel;
using TreeBuilder.Contracts;
using MvvmTreeView;
using System.Windows.Controls;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows;

namespace TreeBuilder.ViewModels
{
    public class TreeBuilderTreeNodeAttributeNode : TreeBuilderTreeNodeItemNode
    {
        private ITreeNodeAttributeNode attributeNode;
        private bool allowDrop;
        public override event PropertyChangedEventHandler PropertyChanged;

        public TreeBuilderTreeNodeAttributeNode(ITreeBuilderService treeBuilderService, ITreeView treeView, ITreeNodeAttributeNode attributeNode, ITreeNode parent) : base(treeBuilderService, treeView, parent)
        {
            this.attributeNode = attributeNode;
            this.Tag = attributeNode;
            this.allowDrop = true;

            UpdatePropertyTypes();
            UpdateMappingTypes();
        }

        public override ITreeNodeBase InternalNodeObject
        {
            get
            {
                return attributeNode;
            }
        }

        public override void UpdatePropertyTypes()
        {
            var invokeOperation = treeBuilderService.GetItemBindingPropertyTypes(attributeNode);

            invokeOperation.Invoked += (sender, e) =>
            {
                this.currentPropertyTypes = e.ReturnValue;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("ContextMenu"));
                }
            };
        }

        public override void UpdateMappingTypes()
        {
            var invokeOperation = treeBuilderService.GetTransferCreationMappingTypes(attributeNode);

            invokeOperation.Invoked += (sender, e) =>
            {
                this.currentMappingTypes = e.ReturnValue;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("ContextMenu"));
                }
            };
        }

        public override void Expand()
        {
            if (!this.IsChildrenLoaded)
            {
                attributeNode.ChildrenLoaded += new TreeBuilder.Contracts.ChildrenLoadedHandler(OnChildrenLoaded);
                attributeNode.LoadChildren();
            }

            base.Expand();
        }

        public override ITreeNodeBase Node
        {
            get
            {
                return attributeNode;
            }
            set
            {
                attributeNode = (ITreeNodeAttributeNode)value;
            }
        }

        public override IBase BaseObject
        {
            get 
            {
                return attributeNode.BaseObject;
            }

            set
            {
                attributeNode.BaseObject = value;
            }
        }

        public override string ID
        {
            get 
            {
                return attributeNode.ID;
            }
        }

        public override void LoadChildren()
        {
            attributeNode.ChildrenLoaded += new TreeBuilder.Contracts.ChildrenLoadedHandler(OnChildrenLoaded);
            attributeNode.LoadChildren();
        }

        public override IEnumerable<ITreeNodeBase> Children
        {
            get 
            {
                return attributeNode.Children;
            }
        }

        public override bool? HasChildren
        {
            get { throw new NotImplementedException(); }
        }

        public override void Remove()
        {
            attributeNode.RemovedSuccessfully += (node, e) =>
            {
                this.parent.Nodes.Remove(this);

                var op = treeBuilderService.OrphansExist();

                op.Completed += (sender, e2) =>
                {
                    if (op.Value)
                    {
                        MessageBox.Show("Orphans exist!");
                    }
                };
            };

            attributeNode.Remove();
        }

        public override string Text
        {
            get
            {
                return this.attributeNode.Text;
            }
            set
            {
                this.attributeNode.Text = value;
            }
        }

        public override bool AllowDrop
        {
            get
            {
                return allowDrop;
            }
            set
            {
                allowDrop = value;
            }
        }


        public override ITreeNode ShallowClone()
        {
            throw new NotImplementedException();
        }

		public override void OnSelect()
		{
		}
        public override ITreeNode DeepClone()
        {
            throw new NotImplementedException();
        }

        public override bool AllowRename
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool HasLayoverImage<T>(out T image)
        {
            return attributeNode.HasLayoverImage<T>(out image);
        }

        public override void AddNew(string shortCut)
        {
        }

        public override void DragEnter(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override void DragLeave(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override void DragOver(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override void DragStarting(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
        {
            e.AllowedEffects = UIObjects.DragDropInterfaces.DragDropEffects.Copy | UIObjects.DragDropInterfaces.DragDropEffects.Link | UIObjects.DragDropInterfaces.DragDropEffects.Move | UIObjects.DragDropInterfaces.DragDropEffects.Scroll;
            e.Effects = UIObjects.DragDropInterfaces.DragDropEffects.Copy;

            e.Handled = true;
        }

        public override void DragCompleted(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
        {
        }

        public override void ItemDropped(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e, int insertionIndex)
        {
        }

        public override void GiveFeedback(object sender, UIObjects.DragDropInterfaces.GiveFeedbackEventArgs e)
        {
        }

        public override float ChildOrdinal
        {
            get
            {
                return attributeNode.ChildOrdinal;
            }
            set
            {
                attributeNode.ChildOrdinal = value;
            }
        }
    }
}
